//
//  ExplodeHelper.m
//  Box2DProject
//
//  Created by Thi Huynh on 9/12/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ExplodeHelper.h"

@implementation ExplodeHelper

+ (void)createExplodeWith:(b2World*)world atPoint:(CGPoint)pos radius:(float)radius maxForce:(float)maxForce suction:(BOOL)suction
{
    {
        BodiesInAABBCallback callback;
        b2AABB aabb;
        
        aabb.lowerBound = [MathHelper toMeters:ccp(pos.x - radius, pos.y - radius)];
        aabb.upperBound = [MathHelper toMeters:ccp(pos.x + radius, pos.y + radius)];
        
        world->QueryAABB(&callback, aabb);
        
        std::map<b2Body*, b2Fixture*>::iterator it;
        
        for(it = callback.queryInfo.begin(); it != callback.queryInfo.end(); ++it)
        {
            b2Body* b = (*it).first;    
            
            b2Vec2 b2TouchPosition = [MathHelper toMeters:pos];
            b2Vec2 b2BodyPosition = b2Vec2(b->GetPosition().x, b->GetPosition().y);
            
            float maxDistance = radius/PTM_RATIO;
            CGFloat distance = 0.0f;
            CGFloat strength = 0.0f;
            float force = 0.0f;
            CGFloat angle = 0.0f;
            
            if(suction)//incase suction 
            {
                distance = b2Distance(b2BodyPosition, b2TouchPosition);
                if(distance > maxDistance) 
                    distance = maxDistance - 0.01;
                // Get the strength
                //strength = distance / maxDistance; // Uncomment and reverse these two. and ones further away will get more force instead of less
                strength = (maxDistance - distance) / maxDistance; // This makes it so that the closer something is - the stronger, instead of further
                force  = strength * maxForce;
                
                // Get the angle
                angle = atan2f(b2TouchPosition.y - b2BodyPosition.y, b2TouchPosition.x - b2BodyPosition.x);
                b->ApplyForce(b2Vec2(cosf(angle) * force, sinf(angle) * force), b->GetPosition());
            }
            else//incase not suction
            {
                distance = b2Distance(b2BodyPosition, b2TouchPosition);
                if(distance > maxDistance) distance = maxDistance - 0.01;
                
                strength = (maxDistance - distance) / maxDistance;
                force = strength * maxForce;
                angle = atan2f(b2BodyPosition.y - b2TouchPosition.y, b2BodyPosition.x - b2TouchPosition.x);
                b->ApplyForce(b2Vec2(cosf(angle) * force, sinf(angle) * force), b->GetPosition());
            }
        }
    }
        
}

+ (void)createExplodeAllObjectWith:(b2World*)world atPoint:(CGPoint)pos maxForce:(float)maxForce suction:(BOOL)suction;
{
    
}

+ (void)createExplodeAndParticleWith:(b2World*)world atPoint:(CGPoint)pos radius:(float)radius maxForce:(float)maxForce suction:(BOOL)suction
{
    
}

+ (void)createExplodeAndSplitBodyWith:(b2World*)world atPoint:(CGPoint)pos radius:(float)radius maxForce:(float)maxForce cuts:(int)cuts suction:(BOOL)suction
{
    
}

+ (void)createExplodeAllWith:(b2World*)world atPoint:(CGPoint)pos radius:(float)radius maxForce:(float)maxForce cuts:(int)cuts suction:(BOOL)suction
{
    
}

@end
